package com.guijie.sheji.service.impl;

import com.guijie.sheji.dao.LoginMapper;
import com.guijie.sheji.domain.Admin;
import com.guijie.sheji.domain.Login;
import com.guijie.sheji.domain.User;
import com.guijie.sheji.except.BusinessException;
import com.guijie.sheji.except.BusinessExceptionCode;
import com.guijie.sheji.service.LoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.awt.print.Book;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {

    private static final Logger LOG = LoggerFactory.getLogger(LoginServiceImpl.class);

    @Autowired
    private LoginMapper loginMapper;

    @Override
    public Boolean selectByLoginName(Login loginUser) {
        System.out.println("开始登陆查询");
        List<Login> userList = loginMapper.select(loginUser);
        for (Login userDb : userList) {
            System.out.println("查询结果:" + userDb.toString());
            if (ObjectUtils.isEmpty(userDb)) {
                // 用户名不存在
                LOG.info("用户名不存在, {}", userDb.getLoginName());
                throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
            } else {
                if (userDb.getPassword().equals(loginUser.getPassword())) {
                    // 登录成功
                    return true;
                } else {
                    // 密码不对
                    LOG.info("密码不对, 输入密码：{}, 数据库密码：{}", loginUser.getPassword(), userDb.getPassword());
                    throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
                }
            }
        }
        return false;
    }
}





